package com.xique.door.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xique.common.core.utils.DateUtils;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.door.bean.domain.BuildElevator;
import com.xique.door.bean.request.buildElevator.BuildElevatorListRequest;
import com.xique.door.mapper.BuildElevatorMapper;
import com.xique.door.service.IBuildElevatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author shd
 * @create 2022/3/29 11:25
 */
@Service
public class BuildElevatorServiceImpl extends ServiceImpl<BuildElevatorMapper, BuildElevator> implements IBuildElevatorService {

    @Autowired
    private BuildElevatorMapper elevatorMapper;

    @Override
    public IPage<BuildElevator> pageList(BuildElevatorListRequest request) {

        QueryWrapper<BuildElevator> condition = new QueryWrapper<>();
        if (StringUtils.isNotNull(request.getParkId())) {
            condition.eq("park_id", request.getParkId());
        }
        if (StringUtils.isNotNull(request.getAreaId())) {
            condition.eq("area_id", request.getAreaId());
        }
        if (StringUtils.isNotNull(request.getBuildingId())) {
            condition.eq("building_id", request.getBuildingId());
        }
        if (StringUtils.isNotEmpty(request.getName())) {
            condition.like("name", request.getName());
        }
        if (StringUtils.isNotEmpty(request.getStatus())) {
            condition.eq("status", request.getStatus());
        }
        Page<BuildElevator> page = new Page<>();
        if (StringUtils.isNotNull(request.getPageNum()) && StringUtils.isNotNull(request.getPageSize())) {
            page.setCurrent(request.getPageNum());
            page.setSize(request.getPageSize());
        }
        condition.orderByDesc("update_time");

        return elevatorMapper.selectPage(page, condition);
    }

    @Override
    public List<BuildElevator> list(BuildElevatorListRequest request) {

        QueryWrapper<BuildElevator> condition = new QueryWrapper<>();
        if (StringUtils.isNotNull(request.getParkId())) {
            condition.eq("park_id", request.getParkId());
        }
        if (StringUtils.isNotNull(request.getAreaId())) {
            condition.eq("area_id", request.getAreaId());
        }
        if (StringUtils.isNotNull(request.getBuildingId())) {
            condition.eq("building_id", request.getBuildingId());
        }
        if (StringUtils.isNotEmpty(request.getName())) {
            condition.like("name", request.getName());
        }
        if (StringUtils.isNotEmpty(request.getStatus())) {
            condition.eq("status", request.getStatus());
        }
        condition.orderByAsc("create_time");

        return elevatorMapper.selectList(condition);
    }

    @Override
    public int insert(BuildElevator elevator) {
        elevator.setCreateTime(DateUtils.getNowDate());
        elevator.setCreateBy(SecurityUtils.getUserId());
        elevator.setUpdateTime(DateUtils.getNowDate());
        elevator.setUpdateBy(SecurityUtils.getUserId());
        return elevatorMapper.insert(elevator);
    }

    @Override
    public int update(BuildElevator elevator) {
        elevator.setUpdateTime(DateUtils.getNowDate());
        elevator.setUpdateBy(SecurityUtils.getUserId());
        return elevatorMapper.updateById(elevator);
    }
}
